with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Exceptions;

with GWindows.Base;
with GWindows.Application;
with GWindows.GStrings;

package body GUI.Instances is
  procedure Create_Instance_Window(Top_Window : in out FFXIAlarm_Window_Type;
                                   Window     : in out Instance_Window_Type) is
  begin
    Create_as_Dialog
      (Window.Window, Top_Window.Top_Window,
       "Select a FFXI instance", width => 200, height => 92);
    Small_Icon (window.Window, Resources.Small_Icon);
    Large_Icon (window.Window, Resources.Large_Icon);
    Set_Font(Window.Window);
    Create
      (Window.Instance_Select, Window.Window, 5, 5, 184, 400, Sort => False);
    Create (Window.Ok, Window.Window, "Select", 57, 30, 80, 25);
  end Create_Instance_Window;

  procedure Prompt_Choice(Main_Window : in out FFXIAlarm_Window_Type;
                          Instances   : in result_info_array;
                          Selection   : out Integer) is
    Instance_Window : Instance_Window_Type;

    procedure Do_Accept(Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      Selection := Current(Instance_Window.Instance_Select);
      Close(Instance_Window.Window);
    end Do_Accept;
  begin
    Selection := 0;
    Create_Instance_Window(Main_Window, Instance_Window);
    if Visible(Main_Window.Window) then
      Center(Instance_Window.Window, Main_Window.Window);
    end if;
    for i in Instances'range loop
      Add (Instance_Window.Instance_Select,
        GWindows.GStrings.To_GString_From_String(
          To_String(Instances(i).wTitle)));
    end loop;
    Current (Instance_Window.Instance_Select, 1);
    On_Click_Handler (Instance_Window.OK,
                      Do_Accept'Unrestricted_Access);
    GWindows.Application.Show_Modal (Instance_Window.Window, Main_Window.Window);
  end Prompt_Choice;

  procedure Choose_Instance(Main_Window : in out FFXIAlarm_Window_Type;
                            Result      : out Process_ID;
                            Success     : out Boolean) is
    NO_FFXI : exception;
    n : Integer;
  begin
    Set_Status(Main_Window, "Accessing Final Fantasy XI process...");
    Success := False;
    declare
      instances : constant result_info_array :=
        Enumerate_Windows(Class => "FFXiClass", pName => "pol.exe");
    begin
      if instances'length = 0 then
        Ada.Exceptions.Raise_Exception(No_FFXI'Identity,
          "No running Final Fantasy XI instance has been found.");
      elsif instances'length = 1 then
        Result := instances(1).hProc;
        Success := True;
      else
        Prompt_Choice(Main_Window, instances, n);
        if n = 0 then
          Set_Status(Main_Window, "Idle");
        else
          Result := instances(n).hProc;
          Success := True;
        end if;
      end if;
    end;
  exception
    when E : others =>
      Set_Status(Main_Window, "Error during initialization");
      Message_Box
        (Main_Window,
         "Error during initialization: "&
         Ada.Exceptions.Exception_Name (E)&": "&
         Ada.Exceptions.Exception_Message (E));
  end Choose_Instance;
end GUI.Instances;
